A Wrapper class is a class whose object wraps a primitive data type.
Java provides these classes in the java.lang
package to convert primitive types into
objects.
Primitive Type | Wrapper Class |
---|---|
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
char |
Character |
boolean |
Boolean |
ArrayList
, HashMap
can only
store objects (not primitives)parseInt()
,
compare()
, valueOf()
<T>
) do not support
primitives, only objectsAutoboxing = primitive โก๏ธ object
Unboxing = object โก๏ธ primitive
public class AutoBoxingExample { public static void main(String[] args) { int a = 10; // Autoboxing Integer obj = a; // Unboxing int b = obj; System.out.println("Object: " + obj); System.out.println("Primitive: " + b); } }
Let's say you want to store user ages in a list:
ArrayList<Integer> ages = new ArrayList<>(); ages.add(25); // Autoboxing happens here: int โ Integer ages.add(30); int sum = ages.get(0) + ages.get(1); // Unboxing: Integer โ int System.out.println("Sum of ages: " + sum);
public class WrapperMethods { public static void main(String[] args) { String number = "100"; int value = Integer.parseInt(number); // Convert string to int System.out.println("Parsed value: " + value); String text = Integer.toString(1234); // Convert int to string System.out.println("String value: " + text); boolean result = Boolean.parseBoolean("true"); System.out.println("Boolean value: " + result); char ch = 'A'; System.out.println("Is Digit: " + Character.isDigit(ch)); System.out.println("Is Uppercase: " + Character.isUpperCase(ch)); } }
๐น Question | ๐ฌ Expected Answer |
---|---|
What is a wrapper class? | A class that converts a primitive type into an object. |
Why are wrapper classes needed? | For collections, generics, null handling, utilities. |
Difference between Integer and int? | int is primitive, Integer is a class (object). |
What is autoboxing/unboxing? | Auto conversion between primitive and wrapper. |
Can you store int in ArrayList? |
Not directly โ it uses Integer via autoboxing. |
Generics allows writing code that works with different data types while providing type safety.
Introduced in: Java 5
class Box<T> { T value; void setValue(T val) { value = val; } T getValue() { return value; } }
Box<String> s = new Box<>(); s.setValue("Hi"); System.out.println(s.getValue());
public <T> void print(T[] arr) { for (T t : arr) System.out.println(t); }
class Demo<T extends Number> { void show(T n) { System.out.println(n.doubleValue()); } }
<?>
โ unknown type<? extends Number>
โ any subclass of Number<? super Integer>
โ any superclass of IntegerA fully object-oriented language is one where:
Smalltalk
, Ruby
, Scala
Reason | Explanation |
---|---|
Primitive Types | Java has int , float , char , etc., which are not
objects. |
Static Methods/Variables | Methods like Math.sqrt() don't need objects. |
Limited Operator Overloading | Custom operator overloading is not allowed. |
Top-Level Code Outside Objects | Static context used before object creation (e.g., main method). |
Think of a programming language like a toolkit.
In Java, sometimes it uses tools outside the object world (like int
instead of Integer
), which breaks the pure object-oriented rule.
Whereas in Smalltalk:
3 + 4 "Even 3 is an object, and + is a message"
๐น Question | ๐ฌ Expected Answer |
---|---|
Is Java fully object-oriented? | No, because it uses primitive types and static members. |
Why does Java have primitives? | For performance โ primitives are faster and use less memory. |
What makes Java object-oriented? | Class-based, supports encapsulation, inheritance, polymorphism, and abstraction. |
What breaks pure OOP in Java? | int , boolean , static methods/fields, etc. |
int
, char
, etc.)Integer
, Double
, etc.